package com.ruoyi.project.rf.controller;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.project.bas.msgTray.domain.TrayCodeMsg;
import com.ruoyi.project.bas.productMap.domain.ProductMap;
import com.ruoyi.project.bas.productMap.service.IProductMapService;
import com.ruoyi.project.in.stockindetail.domain.StockinDetail;
import com.ruoyi.project.in.stockindetail.service.StockinDetailServiceImpl;
import com.ruoyi.project.mydo.detail.domain.MydoJobDetail;
import com.ruoyi.project.mydo.mydojob.domain.MydoJob;
import com.ruoyi.project.mydo.mydojob.service.IMydoJobService;
import com.ruoyi.project.rf.domain.BasJxTaiChe;
import com.ruoyi.project.rf.domain.changeSpaceData;
import com.ruoyi.project.rf.service.IPdaService;
import com.ruoyi.project.stock.stock.domain.BasStock;
//import lombok.Synchronized;
import org.apache.commons.math3.stat.descriptive.summary.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物资入库Controller
 *
 * @author wz
 * @date 2021-11-16
 */
@Controller
@RequestMapping("/pda")
@CrossOrigin
public class PdaController extends BaseController {
    private String prefix = "rf";

    @Autowired
    private IPdaService pdaService;

    @Autowired
    private IMydoJobService mydoJobService;

    @Autowired
    private IProductMapService productMapService;

    /**
     * 物资绑定
     *
     * @return
     */
    @GetMapping("/rfproductSure")
    public String rfBack() {
        return prefix + "/rfproductSure";
    }

    /**
     * 托盘入
     *
     * @return
     */
    @GetMapping("/tpIn")
    public String tpIn() {
        return prefix + "/tpIn";
    }


    /**
     * 托盘出
     *
     * @return
     */
    @GetMapping("/tpOut")
    public String tpOut() {
        return prefix + "/tpOut";
    }

    /**
     * 确认入库
     *
     * @return
     */
    @GetMapping("/productIn")
    public String productIn() {
        return prefix + "/productIn";
    }

    /**
     * 回流
     *
     * @return
     */
    @GetMapping("/rfReflux")
    public String rfReflux() {
        return prefix + "/rfReflux";
    }

    /**
     * 拣选台绑盘
     */
    @GetMapping("/pickingIn")
    public String pickingIn() {
        return prefix + "/pickingIn";
    }

    /**
     * 拣选台入库
     */
    @GetMapping("/pickingIn2")
    public String pickingIn2() {
        return prefix + "/pickingIn2";
    }

    /**
     * 拣选台出库
     */
    @GetMapping("/pickingOut")
    public String pickingOut() {
        return prefix + "/pickingOut";
    }

    /**
     * 确认出库
     */
    @GetMapping("/outSure")
    public String outSure() {
        return prefix + "/outSure";
    }

    /**
     * 查询入库单号
     */
    @RequestMapping("/selectInStock")
    @ResponseBody
    public AjaxResult selectInStock(String status, String inType) {
        return AjaxResult.success(pdaService.selectInStockIdByStatus(status, inType));
    }

    /**
     * 根据入库单号查询未完成的入库子单号
     */
    @RequestMapping("/selectInStockDId")
    @ResponseBody
    public AjaxResult selectInStockDId(String stockId) {
        List<String> stockDId = pdaService.selectInStockDIdByStockId(stockId);
        if (stockDId.size() == 0) {
            return AjaxResult.error("单据已全部完成入库");
        } else {
            return AjaxResult.success(stockDId);
        }
    }

    /**
     * 查询物资入库信息
     */
    @RequestMapping("/list")
    @ResponseBody
    public AjaxResult StockinDetail(String stockInDetailId) {
        TrayCodeMsg stockinDetail = pdaService.selectstockinDetailById(stockInDetailId);
        if (stockinDetail == null) {
            System.out.println(JSONObject.toJSON(AjaxResult.error("无该单据")));
            return AjaxResult.error("无该单据");
        }
        return AjaxResult.success(stockinDetail);
    }

    /**
     * 信息绑定
     *
     * @param trayCodeMsg
     * @return
     */
    @RequestMapping("/msgSure")
    @ResponseBody
    public Map<String, String> msgSure(TrayCodeMsg trayCodeMsg) {
        Map<String, String> map = new HashMap();
        if ("".equals(trayCodeMsg.getTrayCode()) || trayCodeMsg.getTrayCode() == null || trayCodeMsg.getTrayCode().length() != 8) {
            map.put("v_result", "500");
            map.put("v_resutmsg", "托盘条码不能为空！");
            System.out.println(JSONObject.toJSON(map));
            return map;
        }
        if ("".equals(trayCodeMsg.getInQuantity()) || trayCodeMsg.getInQuantity() == null) {
            map.put("v_result", "500");
            map.put("v_resutmsg", "入库数量不可为空！");
            System.out.println(JSONObject.toJSON(map));
            return map;
        }


        //todo 加 @RequestBody
        return pdaService.msgSure(trayCodeMsg);
    }

    /**
     * 回流
     *
     * @return
     */
    @RequestMapping("/refluxSure")
    @ResponseBody
    public Map<String, String> refluxSure(String trayCode) {
        Map<String, String> map = new HashMap();
        if (trayCode == null) {
            map.put("v_result", "500");
            map.put("v_resutmsg", "托盘条码不能为空！");
            System.out.println(JSONObject.toJSON(map));
            return map;
        }
        return pdaService.refluxSure(trayCode);

    }

    /**
     * 拣选完出库
     *
     * @return
     */
    @RequestMapping("/selectFinishOut")
    @ResponseBody
    public Map<String, String> selectFinishOut(String trayCode) {
        Map<String, String> map = new HashMap();
        if (trayCode == null) {
            map.put("v_result", "500");
            map.put("v_resutmsg", "托盘条码不能为空！");
            System.out.println(JSONObject.toJSON(map));
            return map;
        }
        return pdaService.selectFinishOut(trayCode);
    }

    /**
     * 出库确认
     *
     * @return
     */
    @RequestMapping("/outStockSure")
    @ResponseBody
    public AjaxResult outStockSure(@RequestBody MydoJob mydoJob) {
        String trayCode = mydoJob.getTrayCode();

        String msg = pdaService.outStockSure(trayCode);
        if ("操作成功！".equals(msg)) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error(msg);
        }
    }

    /**
     * 出库确认之根据托盘查子作业 返回集合
     *
     * @return
     */
    @RequestMapping("/listByTrayCode")
    @ResponseBody
    public AjaxResult listByTrayCode(@RequestBody Map<String, String> params) {
        String trayCode = params.get("trayCode");
        List<MydoJobDetail> mydoJobDetails = pdaService.listByTrayCode(trayCode);
        return AjaxResult.success(mydoJobDetails);
    }

    /**
     * 料箱出库确认
     *
     * @return
     */
    @RequestMapping("/outStockSureL")
    @ResponseBody
    public AjaxResult outStockSureL(@RequestBody Map<String, String> params) {
        String trayCode = params.get("trayCode");

        return pdaService.outStockSureL(trayCode);

    }

    /**
     * 盘点信息查询
     *
     * @return
     */
    @RequestMapping("/checkMsg")
    @ResponseBody
    public AjaxResult checkMsg(@RequestBody Map<String, String> params) {
        String trayCode = params.get("trayCode");

        //根据条码查询明细
        List<MydoJobDetail> detailList = pdaService.checkMsg(trayCode);
        if (detailList.size() > 0) {
            for (int i = 0; i < detailList.size(); i++) {
                if (detailList.get(i).getStation() != null && detailList.get(i).getLayerType() != null) {
                    detailList.get(i).setStation(detailList.get(i).getStation() + detailList.get(i).getLayerType());
                }
                if (detailList.get(i).getStation() == null && detailList.get(i).getLayerType() != null) {
                    detailList.get(i).setStation(" " + detailList.get(i).getLayerType());
                }
                if (detailList.get(i).getStation() != null && detailList.get(i).getLayerType() == null) {
                    detailList.get(i).setStation(detailList.get(i).getStation());
                }

            }

            return AjaxResult.success("操作成功！", detailList);
        } else {
            return AjaxResult.error("无该条码明细，请检查！");
        }
    }

    /**
     * 盘点信息确认
     *
     * @param detailList 明细列表
     * @return
     */
    @RequestMapping("/pdStockSure")
    @ResponseBody
    public AjaxResult pdStockSure(@RequestBody List<MydoJobDetail> detailList) {
        if (detailList.size() <= 0) {
            return AjaxResult.success("请求数据为空！");
        }
        String msg = pdaService.pdStockSure(detailList);
        if ("操作成功！".equals(msg)) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error(msg);
        }
    }

    /**
     * 盘点入库（改盘点确认建出的作业status = 1）
     */
    @RequestMapping("/pdStockSureIn")
    @ResponseBody
    public AjaxResult pdStockSureIn(@RequestBody MydoJob mydoJob) {
        if (mydoJob.getTrayCode() == null) {
            return AjaxResult.success("盘点入库的托盘编码为空");
        }
        String msg = pdaService.pdStockSureIn(mydoJob.getTrayCode());
        if ("操作成功！".equals(msg)) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error(msg);
        }
    }

    /**
     * 空料箱入库
     *
     * @return
     */
    @RequestMapping("/trayLIn")
    @ResponseBody

    public AjaxResult trayLIn(@RequestBody Map<String, String> params) {
        String trayCode = params.get("trayCode");

        return pdaService.trayLIn(trayCode);

    }

    /**
     * 托盘(垛)入库
     *
     * @return
     */
    @RequestMapping("/trayIn")
    @ResponseBody
//    @Synchronized
    public AjaxResult trayIn(@RequestBody Map<String, String> params) {
        String trayCode = params.get("trayCode");
        String quantity = params.get("quantity");


        String msg = pdaService.trayIn(trayCode, Integer.parseInt(quantity));
        if ("提交成功".equals(msg)) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error(msg);
        }
    }

    /**
     * 托盘(垛)出库
     *
     * @return
     */
    @RequestMapping("/trayOut")
    @ResponseBody
    public AjaxResult trayOut(@RequestBody Map<String, String> params) {

        String exitId = params.get("exitId");
        String msg = pdaService.trayOut(exitId);
        if ("提交成功".equals(msg)) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error(msg);
        }
    }

    /**
     * 空料箱出库
     *
     * @return
     */
    @RequestMapping("/trayLOut")
    @ResponseBody
    public AjaxResult trayLOut(@RequestBody Map<String, String> params) {

        String exitId = params.get("exitId");
        String quantity = params.get("quantity");

        return pdaService.trayLOut(exitId, quantity);

    }

    /**
     * 信息查询
     *
     * @return
     */
    @RequestMapping("/trayInfo")
    @ResponseBody
    public AjaxResult trayInfo(@RequestBody Map<String, String> params) {
        String trayCode = params.get("trayCode");

        return pdaService.selectTrayInfo(trayCode);
    }

    @Autowired
    StockinDetailServiceImpl stockinDetailService;

    /**
     * 查询物资入库信息  setProductGuid\product_id(chaxun)
     */
    @RequestMapping("/listByStockinDetailId")
    @ResponseBody
    public AjaxResult listByStockinDetailId(@RequestBody StockinDetail stockinDetail) {
        String productId = stockinDetail.getProductId();
        if (productId != null && !"".equals(productId)) {
            ProductMap productMap = productMapService.selectProductMapByDetail(stockinDetail.getProductId());
            if (productMap != null) {
                stockinDetail.setProductId(productMap.getProductId());
            }
        }
        List<StockinDetail> stockinDetails = stockinDetailService.selectStockinDetailList(stockinDetail);
        if (stockinDetails.size() != 1) {
            System.out.println(JSONObject.toJSON(AjaxResult.error("无该单据")));
            return AjaxResult.error("无该单据");
        }

        BigDecimal quantity = stockinDetails.get(0).getQuantity() == null ? BigDecimal.ZERO : stockinDetails.get(0).getQuantity();
        Integer quantityX = stockinDetail.getQuantityX();
        if (quantityX == null) {
            quantityX = 0;
        }
        BigDecimal bigDecimal = new BigDecimal(quantityX);
        BigDecimal subtract = quantity.subtract(bigDecimal);

        StockinDetail stockinDetail1 = stockinDetails.get(0);
        stockinDetail1.setQuantity(subtract);
        return AjaxResult.success(stockinDetail1);
    }

    /**
     * 生成入库作业
     */
    @RequestMapping("/saveJobX")
    @ResponseBody
//    @Synchronized
    public AjaxResult saveJobX(@RequestBody StockinDetail stockinDetail) {
        return pdaService.saveJobX(stockinDetail);
    }

    /**
     * 生成拣选台入库作业
     */
    @RequestMapping("/pickingIn")
    @ResponseBody
    public AjaxResult pickingIn(@RequestBody BasStock basStock) {
        String msg = pdaService.pickingIn(basStock);
        if ("提交成功".equals(msg)) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error("提交失败");
        }
    }

    /**
     * 改拣选台入库作业状态
     */
    @RequestMapping("/pickingIn2")
    @ResponseBody
    public AjaxResult pickingIn2(@RequestBody BasStock basStock) {
        String msg = pdaService.pickingIn2(basStock);
        if ("提交成功".equals(msg)) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error("提交失败");
        }
    }

    /**
     * 生成拣选台出库作业
     */
    @RequestMapping("/pickingOut")
    @ResponseBody
    public AjaxResult pickingOut(@RequestBody BasStock basStock) {
        String msg = pdaService.pickingOut(basStock);
        if ("提交成功".equals(msg)) {
            return AjaxResult.success(msg);
        } else {
            return AjaxResult.error("提交失败");
        }
    }
//    /**
//     * 查询物资入库信息
//     */
//    @RequestMapping("/list")
//    @ResponseBody
//    public AjaxResult StockinDetail(TrayCodeMsg trayCodeMsg) {
//        //todo 加@RequestBody
//        System.out.println(JSONObject.toJSON(trayCodeMsg));
//
//           List<StockinDetail> stockinDetailList = pdaService.selectstockinDetail(trayCodeMsg);
//
//        if (stockinDetailList.size() ==0) {
//            System.out.println(JSONObject.toJSON(AjaxResult.error("无该单据")));
//            return AjaxResult.error("无该单据");
//        }
//
//        return AjaxResult.success(stockinDetailList);
//    }

    /*
     * 料相入到机械手,绑定信息
     * 参数：料箱码、物料码、数量
     * */
    @RequestMapping("/inTemp")
    @ResponseBody
    public AjaxResult inTemp(@RequestBody Map<String, String> params) {

        AjaxResult outJob = pdaService.inTemp(params);

        return outJob;
    }

    /*
     * 完成
     * */
    @RequestMapping("/inTempFinish")
    @ResponseBody
    public AjaxResult inTempFinish(@RequestBody Map<String, String> params) {

        AjaxResult outJob = pdaService.inTempFinish(params);

        return outJob;
    }

    /*
     * 确认
     * */
    @RequestMapping("/inTempSure")
    @ResponseBody
    public AjaxResult inTempSure(@RequestBody Map<String, String> params) {

        AjaxResult outJob = pdaService.inTempSure(params);

        return outJob;
    }


    /*
     * 托盘转换100-99
     * */
    @RequestMapping("/changeSpace")
    @ResponseBody
    public AjaxResult changeSpace(@RequestBody changeSpaceData params) {

        AjaxResult xx = pdaService.changeSpace(params.getTrayCode1(), params.getTrayCode(), params.getSelectCargo());

        return xx;
    }


    /**
     * 手持机出库确认
     *
     * @param params
     * @return
     */
    @RequestMapping("/ckSure")
    @ResponseBody
    public AjaxResult ckSure(@RequestBody JSONObject params) {
        String warehouseId = (String) params.get("warehouseId");
        String trayCode = (String) params.get("trayCode");
        String jxTrayCode = (String) params.get("JxTrayCode");
        String newTarSite = (String) params.get("newTarSite");
        List<Map<String,Object>> maplist = (List<Map<String,Object>>)params.get("data");
        for (Map<String, Object> stringObjectMap : maplist) {
            String taskId = (String) stringObjectMap.get("taskId");
            String serialNo = (String) stringObjectMap.get("serialNo");
            pdaService.updateSerialNoByTaskId(taskId, serialNo);
        }
//        return pdaService.outSure(trayCode, jxTrayCode,newTarSite);
        //System.err.println("warehouseId:"+warehouseId+",trayCode:"+trayCode+",jxTrayCode:"+jxTrayCode);
        return AjaxResult.success();
    }


    /**
     * 手持机
     *
     * @param params
     * @return
     */
    @RequestMapping("/ktpIn")
    @ResponseBody
    public AjaxResult ktpIn(@RequestBody JSONObject params) {

        String trayCode = (String) params.get("trayCode");
        String entrance = (String) params.get("entrance");
        return pdaService.addKTPIn(trayCode, entrance);
    }


    /**
     * 台车类型
     *
     * @param params
     * @return
     */

    @RequestMapping("/selectTray")
    @ResponseBody
    public AjaxResult selectTray(@RequestBody JSONObject params) {

        String trayCode = (String) params.get("trayCode");
        List<BasJxTaiChe> basJxTaiChes = pdaService.selectTray(trayCode);

        return AjaxResult.success(basJxTaiChes);
    }


    @RequestMapping("/submitTaiChe")
    @ResponseBody
    public AjaxResult submitTaiChe(@RequestBody JSONObject params) {

        String podCode = (String) params.get("podCode");
        String entranceId = (String) params.get("entranceId");

        return AjaxResult.success();
//        return pdaService.submitTaiChe(podCode, entranceId);
    }

    @RequestMapping("/ktpInStock")
    @ResponseBody
    public AjaxResult ktpInStock(@RequestBody JSONObject params) {

        String trayCode = (String) params.get("trayCode");
        String theFloor = (String) params.get("theFloor");
        String thePlatoon = (String) params.get("thePlatoon");

        return pdaService.ktpInStock(trayCode, theFloor,thePlatoon);
    }


    /**
     * 集货申请
     * @param params
     * @return
     */
    @RequestMapping("/storeGoods")
    @ResponseBody
    public AjaxResult storeGoods(@RequestBody JSONObject params) {

        String trayCode = (String) params.get("trayCode");

        return pdaService.storeGoods(trayCode);
    }




}
